/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Lab_v3;

/**
 *
 * @author salman
 */
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class read_xml_file {

   private String[] default_data = {};
   private Document doc;
   private dbtables_test_metadata dbobject_test_metadata;

 public read_xml_file() {


 try {

    File fXmlFile = new File("src/default_table_data.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();


  } catch (Exception e) {
     System.err.println(e.getMessage());
     // e.printStackTrace();
  }
 }

 private static String getTagValue(String sTag, Element eElement){
    NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
    Node nValue = (Node) nlList.item(0);

    return nValue.getNodeValue();
 }

 public void populateTestMetadataTable()
    {
       //  System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    dbtables_test_metadata dbobject_test_metadata =  new dbtables_test_metadata("sqlite","mcqTest.db");
    NodeList nList = doc.getElementsByTagName("test_metadata");
    //System.out.println("-----------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {

       Node nNode = nList.item(temp);
       if (nNode.getNodeType() == Node.ELEMENT_NODE) {

          Element eElement = (Element) nNode;
          dbobject_test_metadata.insertSingleEntry(getTagValue("test_id",eElement), getTagValue("test_name",eElement), getTagValue("test_description",eElement));

//          System.out.println("First Name : "  + getTagValue("test_id",eElement));
//          System.out.println("Last Name : "  + getTagValue("test_name",eElement));
//          System.out.println("Nick Name : "  + getTagValue("test_description",eElement));
        //  System.out.println("Salary : "  + getTagValue("salary",eElement));

        }
    }
   //  dbobject_test_metadata.insertSingleEntry("10","sddsfdfsd", "sdfsdfsdfsd");
   dbobject_test_metadata.viewAllEntries();
 }


  public void populateQuestionMetadataTable()
    {
       //  System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    dbtables_question_metadata dbobject_question_metadata =  new dbtables_question_metadata("sqlite","mcqTest.db");
    NodeList nList = doc.getElementsByTagName("question_metadata");
    //System.out.println("-----------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {

       Node nNode = nList.item(temp);
       if (nNode.getNodeType() == Node.ELEMENT_NODE) {

          Element eElement = (Element) nNode;
          dbobject_question_metadata.insertSingleEntry(getTagValue("question_id",eElement), getTagValue("question_number",eElement),getTagValue("question_string",eElement), getTagValue("test_id",eElement));

//          System.out.println("First Name : "  + getTagValue("test_id",eElement));
//          System.out.println("Last Name : "  + getTagValue("test_name",eElement));
//          System.out.println("Nick Name : "  + getTagValue("test_description",eElement));
        //  System.out.println("Salary : "  + getTagValue("salary",eElement));

        }
    }
   //  dbobject_test_metadata.insertSingleEntry("10","sddsfdfsd", "sdfsdfsdfsd");
   dbobject_question_metadata.viewAllEntries();
 }


    public void populateMCQOptionsTable()
    {
       //  System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    dbtables_mcq_options dbobject_mcq_options =  new dbtables_mcq_options("sqlite","mcqTest.db");
    NodeList nList = doc.getElementsByTagName("mcq_options");
    //System.out.println("-----------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {

       Node nNode = nList.item(temp);
       if (nNode.getNodeType() == Node.ELEMENT_NODE) {

          Element eElement = (Element) nNode;
          dbobject_mcq_options.insertSingleEntry(getTagValue("mcq_id",eElement), getTagValue("mcq_aplha_option",eElement),getTagValue("option_value",eElement), getTagValue("is_right",eElement), getTagValue("question_id",eElement));

//          System.out.println("First Name : "  + getTagValue("test_id",eElement));
//          System.out.println("Last Name : "  + getTagValue("test_name",eElement));
//          System.out.println("Nick Name : "  + getTagValue("test_description",eElement));
        //  System.out.println("Salary : "  + getTagValue("salary",eElement));

        }
    }
   //  dbobject_test_metadata.insertSingleEntry("10","sddsfdfsd", "sdfsdfsdfsd");
   dbobject_mcq_options.viewAllEntries();
 }

}
